package com.anthonybennis.blender.client;

import java.util.List;

import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author abennis
 */
public class ResponsiveGridPanel 
{
	private final IGridDataGenerator _dataGenerator;
	private final IGridWidgetFactory _componentFactory;
	
	/**
	 * 
	 * @param _compontFactory
	 */
	public ResponsiveGridPanel(IGridDataGenerator dataGenerator, IGridWidgetFactory componentFactory)
	{
		_dataGenerator = dataGenerator;
		_componentFactory = componentFactory;
	}
	
	/**
	 * 
	 * @return
	 */
	public Widget createWidget() 
	{
		final FlowPanel tileContainerPanel = new FlowPanel(); 
		tileContainerPanel.setWidth("100%");
		tileContainerPanel.setHeight("100%");
		tileContainerPanel.getElement().getStyle().setMargin(0.5, Unit.EM);
		tileContainerPanel.setStyleName("responsive-grid-panel");
		
		final List<String> gridDataList = _dataGenerator.createGridData();
		Widget component;
		for (String data : gridDataList) 
		{
			component = _componentFactory.create(data);
			component.getElement().getStyle().setFloat(Float.LEFT); // Forces FlowPanel to act like HorizontalPanel.
			component.getElement().getStyle().setMargin(0.5, Unit.EM);
			tileContainerPanel.add(component);
		}
		
		final ScrollPanel scrollPanel = new ScrollPanel(tileContainerPanel);
		scrollPanel.setWidth("100%");
		scrollPanel.setHeight("100%");
		
		return scrollPanel;
	}
}